package com.naodong.shenluntiku.mvp.view.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerSubjectWrongComponent;
import com.naodong.shenluntiku.di.module.SubjectWrongModule;
import com.naodong.shenluntiku.mvp.contract.SubjectWrongContract;
import com.naodong.shenluntiku.mvp.model.bean.PagingBean;
import com.naodong.shenluntiku.mvp.model.bean.SubjectInfo;
import com.naodong.shenluntiku.mvp.model.data.api.CommonApi;
import com.naodong.shenluntiku.mvp.presenter.SubjectWrongPresenter;
import com.naodong.shenluntiku.mvp.view.adapter.SubjectHistoryListAdapter;
import com.naodong.shenluntiku.mvp.view.widget.ErrorView;

import java.util.List;

import butterknife.BindView;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.util.UiUtils;


/**
 * 通过Template生成对应页面的MVP和Dagger代码,请注意输入框中输入的名字必须相同
 * 由于每个项目包结构都不一定相同,所以每生成一个文件需要自己导入import包名,可以在设置中设置自动导入包名
 * 请在对应包下按以下顺序生成对应代码,Contract->Model->Presenter->Activity->Module->Component
 * 因为生成Activity时,Module和Component还没生成,但是Activity中有它们的引用,所以会报错,但是不用理会
 * 继续将Module和Component生成完后,编译一下项目再回到Activity,按提示修改一个方法名即可
 * 如果想生成Fragment的相关文件,则将上面构建顺序中的Activity换为Fragment,并将Component中inject方法的参数改为此Fragment
 */

/**
 * Created by hujie on 17/5/23.
 */

public class SubjectWrongActivity extends BaseMVPActivity<SubjectWrongPresenter> implements SubjectWrongContract.View, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    String moreUrl;


    SubjectHistoryListAdapter mAdapter;

    @BindView(R.id.errorView)
    ErrorView errorView;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSubjectWrongComponent
                .builder()
                .appComponent(appComponent)
                .subjectWrongModule(new SubjectWrongModule(this)) //请将SubjectHistoryModule()第一个首字母改为小写
                .build()
                .inject(this);
    }

    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        swipeRefreshLayout.setOnRefreshListener(this);
        firstRequest();
    }

    @Override
    protected int layoutId() {
        return R.layout.a_subject_history;
    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void onRequesrStart() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void onRequestFinish() {
        swipeRefreshLayout.setRefreshing(false);
        if (mAdapter != null && mAdapter.isLoading()) {
            mAdapter.loadMoreComplete();
        }
    }


    private void initRecycleView() {

        UiUtils.configRecycleView(recyclerView, new LinearLayoutManager(this));
        mAdapter = new SubjectHistoryListAdapter();
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.disableLoadMoreIfNotFullPage();
        mAdapter.setPreLoadNumber(3);
        mAdapter.setOnLoadMoreListener(() -> {
            if (moreUrl != null) {
                mPresenter.requestSubjectWrongList(false, moreUrl);
            } else {
                mAdapter.loadMoreEnd();
            }
        }, recyclerView);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            SubjectInfo subjectInfo = (SubjectInfo) adapter.getItem(position);

            startActivity(SubjectInfoActivityAutoBundle.builder(subjectInfo).build(this));

        });
    }


    private void firstRequest() {
        String url = CommonApi.APP_URL + "api/user/choice/wrong";
        mPresenter.requestSubjectWrongList(true, url);
    }

    @Override
    public void onRefresh() {
        firstRequest();
    }

    @Override
    public void setSubjectWrongList(boolean isRefresh, PagingBean<List<SubjectInfo>> listPagingBean) {
        if (mAdapter == null) {
            initRecycleView();
        }
        errorView.hideAllView();
        if (isRefresh) {
            mAdapter.setNewData(listPagingBean.getData());
        } else {
            if (listPagingBean.getData() != null && listPagingBean.getData().size() > 0) {
                mAdapter.addData(listPagingBean.getData());

            }
        }
        checkHasMore(listPagingBean);
    }

    @Override
    public void showEmptyView(String emptyText) {
        errorView.showEmptyView(emptyText);
    }


    private void checkHasMore(PagingBean pagingBean) {
        moreUrl = pagingBean.getNextUrl();
        if (moreUrl != null) {
            mAdapter.setEnableLoadMore(true);
            mAdapter.loadMoreComplete();
        } else {
            mAdapter.loadMoreEnd();
        }
    }

}